Skip to content

Automate user curation #6590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kitsune/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ def filter_exceptions(event, hint):
DMS_FIX_CURRENT_REVISIONS = config("DMS_FIX_CURRENT_REVISIONS", default=None)
DMS_COHORT_ANALYSIS = config("DMS_COHORT_ANALYSIS", default=None)
DMS_UPDATE_L10N_CONTRIBUTOR_METRICS = config("DMS_UPDATE_L10N_CONTRIBUTOR_METRICS", default=None)
DMS_CLEANUP_EXPIRED_USERS = config("DMS_CLEANUP_EXPIRED_USERS", default=None)

PROD_DETAILS_CACHE_NAME = "product-details"
PROD_DETAILS_STORAGE = config(
Expand Down Expand Up @@ -1331,3 +1332,5 @@ def filter_exceptions(event, hint):

SUMO_BOT_USERNAME = config("SUMO_BOT_USERNAME", default="SumoBot")
SUMO_CONTENT_GROUP = config("SUMO_CONTENT_GROUP", default="Staff Content Team")

USER_INACTIVITY_DAYS = config("USER_INACTIVITY_DAYS", default=1095, cast=int)
27 changes: 27 additions & 0 deletions kitsune/users/management/commands/cleanup_expired_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import timedelta

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.utils import timezone

from kitsune.users.utils import delete_user_pipeline


class Command(BaseCommand):
help = "Delete users who haven't logged in for more than settings.USER_INACTIVITY_DAYS days"

def handle(self, *args, **options):
User = get_user_model()
expiration_date = timezone.now() - timedelta(days=settings.USER_INACTIVITY_DAYS)

expired_users = User.objects.filter(last_login__lt=expiration_date)
self.stdout.write(f"Found {expired_users.count()} expired users")

for user in expired_users:
delete_user_pipeline(user)
self.stdout.write(f"Deleted user {user.username}")

self.stdout.write(
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users")
Copy link
Preview

Copilot AI Mar 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final success message calls expired_users.count() after users have been deleted, which may result in an incorrect count (likely 0). Consider storing the initial count before processing the deletions and using that stored value in the success message.

Suggested change
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users")
self.style.SUCCESS(f"Successfully processed {expired_users_count} expired users")

Copilot uses AI. Check for mistakes.

)
17 changes: 17 additions & 0 deletions scripts/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from subprocess import check_call

import babis
import waffle
from apscheduler.schedulers.blocking import BlockingScheduler
from django.conf import settings
from django.utils import timezone
Expand Down Expand Up @@ -399,6 +400,22 @@ def job_cleanup_old_account_events():
call_command("cleanup_old_account_events")


@scheduled_job(
"cron",
month="*",
day="*",
hour="03",
minute="00",
day_of_week=0,
max_instances=1,
coalesce=True,
skip=(settings.READ_ONLY or not waffle.switch_is_active('cleanup-expired-users')),
)
@babis.decorator(ping_after=settings.DMS_CLEANUP_EXPIRED_USERS)
def job_cleanup_expired_users():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a waffle flag/switch to control when this is enabled through admin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

call_command("cleanup_expired_users")


def run():
try:
schedule.start()
Expand Down